/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.string
{
	import com.edi.runtime.component.EDIApplication;
	
	public class StrUnit
	{
		public static var leftSpecialChar:String = "~lt;";
		public static var rightSpecialChar:String = "~gt;";
		
		/**
		 * 替换函数 
		 * @param value 要替换的字符串
		 * @param oldStr 原字符串
		 * @param newStr 替换后的字符串
		 * @return 
		 * 
		 */		
		public static function replace(value:String,oldStr:String,newStr:String):String
		{
			if(value!=null){
				value=value.split(oldStr).join(newStr); 
			}
			return value; 
		}
		
		public static function replaceRevert(value:String):String
		{
			EDIApplication.outputDebug("value0: "+value);
			if(value!=null){
				value=value.split("<").join("&lt;");
				 value=value.split(">").join("&gt;");
			}
			EDIApplication.outputDebug("value1: "+value);
			return value; 
		}
		
		/**
		 * 将数据中的特殊字符替换为正常字符.
		 *  ~lt; --> <
		 *  ~gt; --> >
		 * @param value
		 * @return 
		 * 
		 */		
		public static function replaceSpecialToChar(value:String):String{
			if(value!=null){
				value=value.split(leftSpecialChar).join("<");
			}
			if(value!=null){
				value=value.split(rightSpecialChar).join(">");
			}
			return value;	
		}
		
		/**
		 * 将数据中的正常字符替换为特殊字符.
		 *   < --> ~lt; 
		 *   > --> ~gt; 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function replaceSpecialCharRevert(value:String):String{
			if(value!=null){
				value=value.split("<").join(leftSpecialChar);
			}
			if(value!=null){
				value=value.split(">").join(rightSpecialChar);
			}
			return value;	
		}
		
		/**
		 * 转为xml 
		 * @param value 要转换的字符
		 * @return 
		 * 
		 */		
		public static function replaceAllToXML(value:String):String
		{
			if(value!=null){
				value=replace(value,"&amp;","&");
				value=replace(value,"&gt;",">");
				value=replace(value,"&lt;","<");
				value=replace(value,"&apos;","'");
				value=replace(value,"&quot;","\"");
			}
			return value;
		}
		
		/**
		 * 替换特殊字符 
		 * @param value   要转换的字符
		 * @return 
		 * 
		 */		
		public static function replaceSpecialChar(value:String):String
		{
			if(value!=null)
			{
				value=replace(value,"&","_");
				value=replace(value,">","》");
				value=replace(value,"<","《");
				value=replace(value,"\"","'");
			}
			return value;
		}
		
		/**
		 * 获得域名 
		 * @param value 网页地址
		 * @return 
		 * 
		 */		
		public static function getDomainUrl(value:String):String
		{
			var index:int=0;
			var location:int=0;
			while(location>-1 && index<4)
			{
				location=value.indexOf("/",location+1);
				index++;
			}
			return value.substr(0,location+1);
		}
		
		public static function xmlSecurityPolicy(value:String):Boolean
		{
			return !(value.indexOf("<")>-1 || value.indexOf("&")>-1 || value.indexOf("\"")>-1 || value.indexOf(">")>-1);
		}
	}
}